package online.flowerinsnow.minecraft.bukkit.manhunt.util;

import online.flowerinsnow.minecraft.bukkit.manhunt.event.PlayerDiedEvent;
import online.flowerinsnow.minecraft.bukkit.manhunt.runnable.DamagerRecordRunnable;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.PlayerInventory;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;

public class StateUtils {
    public static void clearInventory(Player player) {
        PlayerInventory inv = player.getInventory();
        for (int i = 0; i < 41; i++) {
            inv.setItem(i, null);
        }
        player.setExp(0);
        player.setLevel(0);
        player.setHealth(20);
        player.setFoodLevel(20);
        player.setSaturation(20);
        player.getActivePotionEffects().forEach(p -> player.removePotionEffect(p.getType()));
        Manhunt.runTask(() -> player.setFireTicks(0));
    }
    public static void clearTempState(Player player) {
        clearInventory(player);
        player.setBedSpawnLocation(null);
    }
    public static void clearAllState(Player player) {
        Bukkit.getPluginManager().callEvent(new PlayerDiedEvent(player, EntityDamageEvent.DamageCause.SUICIDE, DamagerRecordRunnable.getLastDamager(player)));
//        clearInventory(player);
        clearTempState(player);
        Manhunt.getScoreboardManager().unregisterPlayerScoreboard(player);
        Bukkit.getOnlinePlayers().forEach(p -> Manhunt.getScoreboardManager().getPlayerScoreboard(p).unJoin(player.getName()));
        Manhunt.getTeamManager().setPlayerTeam(player, null);
        Manhunt.getBossBarManager().unregisterPlayerBossBar(player);
    }
    public static boolean isInArena(Player player) {
        return switch (player.getWorld().getName().toLowerCase()) {
            case "arena", "nether", "theend" -> true;
            default -> false;
        };
    }
}
